package com.atguigu.gulimall.search.thread;

import java.util.concurrent.*;

/**
 * @author: xuesm
 * @date: 2025/1/9 11:37
 */
public class ThreadTest {

    public static ExecutorService executorService = Executors.newFixedThreadPool(5);

    public static void main(String[] args) throws Exception{
        System.out.println("main...start...");
        CompletableFuture<Object> future1= CompletableFuture.supplyAsync(() -> {
            System.out.println("获取图片属性完成...");
            return "hello jpg";
        }, executorService);
        CompletableFuture<Object> future2= CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品属性完成...");
            return "黑色4G 256G";
        }, executorService);
        CompletableFuture<Object> future3= CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品介绍完成...");
            return "华为";
        }, executorService);
        CompletableFuture<Object> future = CompletableFuture.anyOf(future1, future2, future3);
        System.out.println("main...end..."+ future.get());
    }



    public static void testThread(String[] args) {

        System.out.println("main...start...");
        Runnable01 runnable = new Runnable01();
        new Thread(runnable).start();
        System.out.println("main...end...");

        Callable01 callable01 = new Callable01();
        FutureTask<Integer> futureTask = new FutureTask<>(callable01);
        new Thread(futureTask).start();
        executorService.execute(runnable);
    }

    public static class Thread01 extends Thread{
        @Override
        public void run() {
            System.out.println("当前线程：" + Thread.currentThread().getName());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
        }
    }

    public static class Runnable01 implements Runnable{
        @Override
        public void run() {
            System.out.println("当前线程：" + Thread.currentThread().getName());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
        }
    }

    public static class Callable01 implements Callable<Integer>{

        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程：" + Thread.currentThread().getName());
            int i = 10 / 2;
            return i;
        }
    }
}
